require.config({
    paths: {
        'vue': '../encaptool/encvue',
        'iview': '../utiltool/iview/iview',
        'jquery': '../utiltool/jquery/jquery.min',
        'router': '../encaptool/encarouter',
        'axios': '../encaptool/encaxios'
    },
    shim: {
        iview: [
            'vue'
        ]
    }
})
require(['vue', 'iview', 'jquery', 'router', 'axios'], function (Vue, iview, $, Router, axios) {
    Vue.use(iview);
    new Vue({
        el: '.completed',
        data() {
            return {
            	freightId: '',
            	completedCl: [
            		{
            			title: '序号',
            			type: 'index',
            			align: 'center',
            			width: 150
            		},
            		{
            			title: '送货单号',
            			key: 'freightId',
            			align: 'center',
                        width: 150
            		},
                    {
                        title: '目的地',
                        key: 'bourn',
                        align: 'center',
                        width: 400
                    },
            		{
            			title: '订单产生时间',
            			key: 'createdate',
            			align: 'center',
                        width: 150
            		},
            		{
            			title: '接受订单时间',
            			key: 'acceptdate',
            			align: 'center',
                        width: 170
            		},
            		{
            			title: '结束订单时间',
            			key: 'enddate',
            			align: 'center',
                        width: 170
            		},
            		{
            			title: '公里(约)',
            			key: 'kilometre',
            			align: 'center',
                        width: 110
            		},
            		{
            			title: '运费系数',
            			key: 'coefficient',
            			align: 'center',
                        width: 150
            		},
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        width: 150,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'large'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.index);
                                        }
                                    }
                                }, '详情')
                            ]);
                        }
                    }
            	],
                // 全部信息
            	completedData: [],
                // 初始化信息总条数
                dataCount: 0,
                // 每页显示多少条
                pageSize: 50,
                // 每页信息
                historyData: [],
            	loading: false,
                // 详情信息
                detailedData: []
            };
        },
        methods: {
            // 获取已完成订单
        	getCompleted (index) {
        		this.loading = true;
        		var setting = {
        			method: 'get',
        			url: '/YF/public/api/getCompleted/' + index
        		};
        		axios.call(this, function(res){
        			if(res.code === 0){
        				this.historyData = res.data[1];
                        this.dataCount = res.data[0];
                        // // 初始化显示，小于每页显示条数，全显，大于每页显示条数，取前每页条数显示
                        // if(this.dataCount < this.pageSize) {
                        //     this.historyData = this.completedData;
                        // }else {
                        //     this.historyData = this.completedData.slice(0, this.pageSize);
                        // }
        				this.loading = false;
        			}
        			else{
        				this.$Message.error('数据为空');
        				this.loading = false;
        			}
        		}.bind(this), {
        			setting: setting
        		});
        	},
            // 切换页码
            changePage (index) {
                var _start = ( index - 1 ) * this.pageSize;
                var _end = index * this.pageSize;
                this.historyData = this.completedData.slice(_start,_end);
                this.getCompleted(index);
            },
            // 查询
        	query (index) {
        		if(this.freightId == ''){
        			this.$Message.error('订单号不能为空');
        		}
        		else{
        			this.loading = true;
                    {
        				var setting = {
        					method: 'get',
        					url: '/YF/public/api/queryByFreightId/' + index + '/' + this.freightId
        				}
        			}
        			axios.call(this, function(res){
        				if(res.code !== 0){
        					this.$Message.error('没有查找到此订单，请重新输入');
        				}
        				this.historyData = res.data[1];
                        this.dataCount = res.data[0];
        				this.loading = false;
        				this.freightId='';
        			}.bind(this), {
        				setting: setting
        			});
        		}
        	},
            // 显示全部
        	selectAll () {
        		this.getCompleted(1);
        	},
            // 详情
            show(index) {
                console.log(this.historyData[index].orderid)
                this.loading = true;
                var setting = {
                    method: 'get',
                    url: '/YF/public/api/getDetailedInfo/'+this.historyData[index].freightId+'/'+this.historyData[index].orderid
                }
                axios.call(this, function(res){
                    if(res.code === 0){
                        this.detailedData = res.data;
                    }
                    else{
                        this.$Message.error('暂无详细信息');
                    }
                    this.loading = false;
                    this.$Modal.info({
                        title: '详细信息',
                        content: `<div style="font-size:13px"><b>
                                  订单号：${this.historyData[index].orderid == null ? '暂无数据' : this.historyData[index].orderid}<br>
                                  客户名称：${this.detailedData[0].customer == null ? '暂无数据' : this.detailedData[0].customer}<br>
                                  板材：${this.detailedData[0].board == null ? '暂无数据' : this.detailedData[0].board}<br>
                                  规格/棱形：${this.detailedData[0].flute == null ? '暂无数据' : this.detailedData[0].flute}<br>
                                  规格型号：${this.detailedData[0].format == null ? '暂无数据' : this.detailedData[0].format}<br>
                                  面积：${this.detailedData[0].area == null ? '暂无数据' : this.detailedData[0].area}<br>
                                  单位面积：${this.detailedData[0].oneofarea == null ? '暂无数据' : this.detailedData[0].oneofarea}<br>
                                  层数：${this.detailedData[0].numberofplies == null ? '暂无数据' : this.detailedData[0].numberofplies}<br>
                                  体积：${this.detailedData[0].volume == null ? '暂无数据' : this.detailedData[0].volume}<br>
                                  备注：${this.detailedData[0].mark == null ? '暂无数据' : this.detailedData[0].mark}</div>`
                    })
                }.bind(this), {
                    setting: setting
                }); 
            },
            // 导出csv
            exportData () {
                var today = new Date();
                var dateStr = today.getFullYear() + (today.getMonth() + 1).toString()
                + today.getDate() + today.getHours()+ today.getMinutes() + today.getSeconds();
                this.$refs.table.exportCsv({
                    filename: '已完成订单' + dateStr,
                    columns: this.completedCl.filter((col, index) => index > 0 && index < 7),
                    data: this.historyData
                });
            }
        },
        mounted () {
        	this.getCompleted(1);
        }
    })
})